import { serverMoment } from "../util/utilities";
import { HCode } from "./code";

export class HError extends Error {
    private code: HCode | number;

    private expectValue?: any;

    private actualValue?: any;
    constructor(code: HCode | number, message?: string, expectValue?: any, actualValue?: any) {
      super(message);

      this.code = code;
      this.expectValue = expectValue;
      this.actualValue = actualValue;
  }

  public toHttpResponseValue(oid?: string): HErrorResponse {
      return {
          st: serverMoment().unix(),
          oid,
          code: this.code,
          expect_value: this.expectValue,
          actual_value: this.actualValue,
          message: this.message
      };
  }

  public static fromHttpResponseValue(value: HErrorResponse): HError {
      return new HError(value.code, value.message, value.expect_value, value.actual_value);
  }

  public toString(oid?: string): string {
      return JSON.stringify(this.toHttpResponseValue(oid), undefined, 2);
  }
}

export interface HErrorResponse {
  code: HCode | number;
  oid?: string;
  st?: number;
  expect_value: any;
  actual_value: any;
  message: string;
}